package com.seagull.core.test.io.nio;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.SocketChannel;
import java.util.Iterator;
import java.util.Set;

/**
 * @author:jian.zhou
 * @DATE: 2018/1/2.
 * @Version:0.0.1
 * @Desc:
 */
public class NioClient {

    private static  int blockSize = 4096 ;

    private static ByteBuffer sendBuffer = ByteBuffer.allocate(blockSize);

    private static ByteBuffer receiveBuffer = ByteBuffer.allocate(blockSize);


    private final static InetSocketAddress serverAddress = new InetSocketAddress("127.0.0.1",7080);

    public static void main(String[] args) {

        try {
            SocketChannel socketChannel =  SocketChannel.open();
            socketChannel.configureBlocking(false);

            //打开选择器
            Selector selector =  Selector.open();
            //连接服务端
            socketChannel.connect(serverAddress);
            //注册链接事件
            socketChannel.register(selector, SelectionKey.OP_CONNECT);
            Set<SelectionKey> selectionKeys ;
            Iterator<SelectionKey> iterator ;
            SelectionKey selectionKey;
            SocketChannel client ;
            String receiveText ;
            String sendText ;


            while ( true ){

                //获得事件列表
                selectionKeys = selector.selectedKeys();

                iterator = selectionKeys.iterator();

                while ( iterator.hasNext()){
                    selectionKey =  iterator.next();
                    client = (SocketChannel) selectionKey.channel();

                    if ( selectionKey.isConnectable() ){
                        System.out.println("客户端开始发起链接了");

                        if ( client.isConnectionPending() ){
                            client.finishConnect();
                            System.out.println("完成链接操作");

                            sendBuffer.clear();
                            sendBuffer.put(" 客户端发送数据 ======》》 ".getBytes());
                            sendBuffer.flip();
                            client.write(sendBuffer);

                        }
                        client.register(selector,SelectionKey.OP_READ);
                    }

                    if ( selectionKey.isReadable() ){

                        receiveBuffer.clear();

                        int count = client.read(receiveBuffer);

                        if ( count > 0 ){
                            receiveText = new String(receiveBuffer.array(),0,count);
                            System.out.println("客户端 接收到 数据了 "+ receiveText);

                            client.register(selector,SelectionKey.OP_WRITE);
                        }
                    }


                    if ( selectionKey.isWritable()){

                        sendBuffer.clear();
                        sendText = " 客户端在发 送  数据=========啦啦";
                        sendBuffer.put(sendText.getBytes());
                        sendBuffer.flip();
                        client.write(sendBuffer);
                        client.register(selector,SelectionKey.OP_READ);
                    }

                    selectionKeys.clear();
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }


}
